package com.soundShape.utils
{
	
	
	
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.model.ApplicationModel;
	import com.soundShape.vo.PhotoDataVO;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Image;
	import mx.core.IVisualElement;
	import mx.core.mx_internal;
	import mx.events.DynamicEvent;
	import mx.managers.PopUpManager;

	public class ImageGetter
	{
		
		use namespace mx_internal; //tells Actionscript that mx_internal is a namespace 
		
		[Dispatcher]
		public var dispatcher:IEventDispatcher;
		
		[Inject]
		public var applicationModel:ApplicationModel;
		
		private var loader:Loader;
		private var arrImageResources:Array = [];
		private var arrThumbsImageResources:Array = [];
		private var temp:Array = [];
		private var arrImages:Array;
		private var ndx:int;
		private var totalFileSize:int;
		
		private var file:File;
		
		[Bindable]
		private var bmp:Bitmap;
		
		public function ImageGetter()
		{
		}
		
		[EventHandler(event="SoundShapeEvent.FILES_READY")]
		public function setImageFiles(soundShapeEvent:SoundShapeEvent):void
		{
			arrImages = soundShapeEvent.data;
			
			ndx = 0;
			totalFileSize = 0;
			
			arrImageResources = [];
			arrThumbsImageResources = [];
			
			this.applicationModel.arrImageResources = [];
			this.applicationModel.arrThumbsImageResources = [];
			
			
			if(this.arrImages == null || this.arrImages.length == 0)
					return;
			
			setImages();
		}
		
		
		public function setImages() : void
		{
			
			
			file = arrImages[ndx] as File;
			
			totalFileSize += file.size;
			
			trace("total file size: " + totalFileSize);
			
			if(totalFileSize > 50000000)
			{
				trace("file size limit reached: " + totalFileSize);
				return;
			}
			
			loader = new Loader();
			var urlReq:URLRequest = new URLRequest(file.url);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleImageLoaded);
			loader.load(urlReq);
		}
		
		
		protected function handleImageLoaded(event:Event):void
		{
			
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleImageLoaded);
			
			
			bmp = new Bitmap();
			bmp = loader.content as Bitmap;
			
			var img:Image;
			
			img = new Image();
			img.source = bmp;
			
			/* 
			this photoVO (annotationPhotoVO) is for the 'main' image that would appear  
			in the popup and is used for the photo annotation
			*/
			
			var annotationPhotoVO:PhotoDataVO = new PhotoDataVO();
			annotationPhotoVO = new PhotoDataVO();
			annotationPhotoVO.image = img;
			annotationPhotoVO.file = file;
			annotationPhotoVO.path = file.nativePath;
			annotationPhotoVO.name = file.name;
			annotationPhotoVO.date = file.creationDate;
			annotationPhotoVO.ndx = ndx;
			annotationPhotoVO.employeeID = this.applicationModel.employeeVO.id;
			
			this.applicationModel.arrImageResources.push(annotationPhotoVO);
			
			if(img.source == null)
					return;
			
			/* 
			why am I doing this?
			to create a thumbnail image a copy (clone) of the bitmap data is required.
			In this app, when the thumbnail image is clicked on, a larger image is displayed
			in a popup.  If I used the thumbnail image as the source for the image in the popup
			the image would render correctly in the popup, but disappear from the thumbnail
			*/
			
			try
			{
				var clone:BitmapData = Bitmap(img.source).bitmapData.clone(); // and here is where the copy is made
				var bitmap:Bitmap  = new Bitmap(clone);
			}
			catch(e:Error)
			{
				trace("image index: [" + ndx + "] Error: " + e.message);
			}
			
			/* 
			this is the separate 'thumbnailPhotoVO' to hold information to 
			create the thumbnail image so much less info is required 
			*/
			
			var newImage:Image = new Image();
			newImage.source = bitmap;
			
			var thumbnailPhotoVO:PhotoDataVO = new PhotoDataVO();
			thumbnailPhotoVO.image = newImage;
			thumbnailPhotoVO.date = file.creationDate;
			thumbnailPhotoVO.name = file.name;
			thumbnailPhotoVO.ndx = ndx;
			
			this.applicationModel.arrThumbsImageResources.push(thumbnailPhotoVO);
			
			ndx++;
			
			if(ndx == arrImages.length) // finished!
			{
				this.dispatcher.dispatchEvent( new SoundShapeEvent(SoundShapeEvent.IMAGES_READY) );
			}
			else // go back and do it again
			{
				setImages();
			}
			
		}
		
		public function doClone(source:Object):* 
		{ 
			var cloneArray:ByteArray=new ByteArray(); 
			cloneArray.writeObject(source); 
			cloneArray.position = 0; 
		return (cloneArray.readObject()); 
		} 
		
		
	}
}